// 云函数入口文件
const axios = require('axios') // 引入axios
const cloud = require('wx-server-sdk')
cloud.init({
    env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    console.log(event.newfileID, event.type, event.status, event.path)
    try {
        if (event.type == 'first_up') {
            if (event.newfileID) { // 如果是新的单词
                let audioBuffer = await axios({
                    method: 'get',
                    url: event.path,
                    responseType: 'arraybuffer',
                    headers: {
                        "Content-Type": "*"
                    },
                })
                let {
                    fileID
                } = await cloud.uploadFile({
                    cloudPath: `Audio/${event.word}.mp3`,
                    fileContent: Buffer.from(audioBuffer.data)
                })
                console.log(fileID)
                await db.collection('userUpfile')
                    .add({
                        data: {
                            content: event.currword, // 单词
                            lyric: event.lyric,
                            openid: wxContext.OPENID, // 用户id
                            user: event.user, // 用户名称
                            status: event.status,
                            address: fileID, // 云地址
                            alterTime: db.serverDate() // 上传时间
                        }
                    })
                return fileID
            } else { // 不是新的单词直接使用传过来的云地址
                await db.collection('userUpfile')
                    .add({
                        data: {
                            content: event.currword, // 单词
                            address: event.path, // 云地址
                            lyric: event.lyric,
                            openid: event.openid, // 用户id
                            user: event.user, // 用户名称
                            status: event.status,
                            alterTime: db.serverDate() // 上传时间
                        }
                    })

                return event.path
            }
        } else if (event.type == 'again_up') {
            if (event.newfileID) { // 重新上传的单词并修改音频
                let audioBuffer = await axios({
                    method: 'get',
                    url: event.path,
                    responseType: 'arraybuffer',
                    headers: {
                        "Content-Type": "*"
                    },
                })
                let {
                    fileID
                } = await cloud.uploadFile({
                    cloudPath: `Audio/${event.word}.mp3`,
                    fileContent: Buffer.from(audioBuffer.data)
                })
                console.log(fileID)
                await db.collection('userUpfile')
                    .where({
                        content: event.currword
                    })
                    .update({
                        data: {
                            content: event.currword, // 单词
                            address: fileID, // 云地址
                            lyric: event.lyric,
                            openid: event.openid, // 用户id
                            user: event.user, // 用户名称
                            alterTime: db.serverDate(), // 上传时间
                            status: event.status
                        }
                    })
                return fileID
            } else { // 重新上传的单词只修改歌词
                await db.collection('userUpfile')
                    .where({
                        content: event.currword
                    })
                    .update({
                        data: {
                            content: event.currword, // 单词
                            address: event.path, // 云地址
                            lyric: event.lyric,
                            openid: event.openid, // 用户id
                            user: event.user, // 用户名称
                            alterTime: db.serverDate(), // 上传时间
                            status: event.status
                        }
                    })
                return event.path
            }
        } else if (event.type == 'cs'){
            let audioBuffer = await axios({
                method: 'get',
                url: event.path,
                responseType: 'arraybuffer',
                headers: {
                    "Content-Type": "*"
                },
            })
            let {
                fileID
            } = await cloud.uploadFile({
                cloudPath: `Audio/${event.name}.mp3`,
                fileContent: Buffer.from(audioBuffer.data)
            })
            console.log(fileID)
        }
    } catch (e) {
        return e
    }


}